// packageB/pages/zbt/zbt.js
const app = getApp();
const sendMessage = require('../../../pages/home/home')
const util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
     num:0,
     page:4,
     second:180,
     times:0,
     total:0,
     click1:'',
     click2:'',
     click3:'',
     click4:'',
     click5:'',
     timer:'',
     inter:'',
     grade:0,
     equipmentNumber:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.startInterval();
    var time = options.time;
    this.setData({
      second:time * 60
    })
    this.startCountdown();
  },

  CLOUD: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  HK: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  SJ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  TZL: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  SCH: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  SNSW: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  MGXZ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  JYJ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  TWT: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  TRYC: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  XKDT: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  LWB: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  LYJ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  FZC: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  FZZ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  HCD: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  XYJ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  XWB: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click5:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  HSM: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  HSZ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  BYY: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  XST: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  ZYYJ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click5:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  XGZ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  MFB: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  SZP: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  SZS: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  JXD: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click5:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  XYD: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  HPG: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  JZY: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  LPF: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  DST: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click5:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  DSG: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  KYJ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  LCD: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  XHC: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  KLD: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click5:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  JYD: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  HBK: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  HST: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  HZZ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  SPP: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:5,
      click5:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  SGD: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click1:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  SYZ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click2:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  KLJ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click3:viewText,
      grade:grade
    })
    this.zhaobutong();
  },
  KLQ: function(e){
    var viewText = e.currentTarget.dataset.text;
    var grade = this.data.grade;
    grade = grade +1;
    this.setData({
      total:4,
      click4:viewText,
      grade:grade
    })
    this.zhaobutong();
    var page = this.data.page;
  },
  zhaobutong(){
    var total = this.data.total;
    var oldnum = this.data.num;
    oldnum = oldnum+1;
    this.setData({
      num:oldnum
    });
    if (oldnum == total){
      var oldpage = this.data.page;
      oldpage = oldpage +1;
      if (oldpage == 10){
        this.setData({
          page:0,
          num:0
        });
      } else {
        this.setData({
          page:oldpage,
          num:0
        });
      }

    }
  },
/**倒计时器 */
  startCountdown() {
    var that = this;
    var second = that.data.second;
    var grade = that.data.grade;
    var equipmentNumber = app.nowEquipmentId;
    var score = 0;
    if (second == 0) {
      clearInterval(that.data.timer);
      clearInterval(that.data.inter);
      if (grade <=5){
        score = 5;
      } else if (grade <=10 && grade >5){
        score = 4;
      } else if (grade <=15 && grade >10){
        score = 3;
      } else {
        score = 2;
      }
      wx.navigateTo({
        url: '../../../pages/fraction/fraction?fraction='+score,
      })
    }
    that.data.timer = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      that.startCountdown();
    } , 1000)
    var times = that.data.times;
    times++;
    that.setData({
      times:times,
      equipmentNumber:equipmentNumber
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"找不同",
      openId:openId,
      times:180
    })
    util.saveRecord(this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var second = this.data.second;
    if (second ==0){
      return;
    }
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"找不同",
      openId:openId
    })
    util.saveRecord(this);
  },
  startInterval: function(){
    var that = this;
    that.data.inter= setInterval(function () {
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      console.log("定时任务开启");
    }, 10000)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})